package cn.acewill.yxd.yunxundian.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;

import org.xutils.DbManager;
import org.xutils.x;

import java.util.List;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.YXDAPP;
import cn.acewill.yxd.yunxundian.model.RMain;

/**
 * Created by AAA on 2017/4/24.
 */

public class ScoreCheckBoxAdapter extends BaseAdapter{
    protected YXDAPP yxdapp=new YXDAPP();
    protected DbManager db = x.getDb(yxdapp.getDaoConfig());
    private Context context;
    private List<RMain.TablesBean.RowsBean.RItemsBean> list;
    private int selectItem = -1;
    private int schId;
    private RMain.TablesBean.RowsBean.Q2Bean q2Bean;
    public ScoreCheckBoxAdapter(Context context, RMain.TablesBean.RowsBean.Q2Bean q2Bean, List<RMain.TablesBean.RowsBean.RItemsBean> list, int schId){
        this.context=context;
        this.list=list;
        this.schId = schId;
        this.q2Bean = q2Bean;
    }
    public void  setSelectItem(int position){
        selectItem = position;
        Log.e("===seletctPosition=====",selectItem+"");
    }
    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.score_item_view,parent,false);
            holder=new ViewHolder();
            holder.scoreCheck= (RadioButton) convertView.findViewById(R.id.scoreCheck);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }
        final RMain.TablesBean.RowsBean.RItemsBean bean=list.get(position);
        holder.scoreCheck.setText(bean.getName()+"");

        holder.scoreCheck.setChecked(false);
        if (list.get(position).isCheck()){
            holder.scoreCheck.setChecked(true);

        }else{
            holder.scoreCheck.setChecked(false);
        }

//        tempLostRR rr = null;
//        try {  //数据库中被选中的
//             rr = db.selector(tempLostRR.class).where("schId","=",schId).and("rRowId","=",bean.getReportRowID()).findFirst();
//            if (rr!=null){
//                if (rr.getScoreType()==1){
//                    for (int k = 0;k<list.size();k++){
//                        if (list.get(k).getID()==rr.getSelectItemId()){
//                            selectItem = k;
//                            break;
//                        }
//                    }
//                }
//            }
//        } catch (DbException e) {
//            e.printStackTrace();
//        }
//        if (selectItem == position){
//            if (holder.scoreCheck.isChecked()) { //当前项取消选中，删除
//

//            }else {
//                holder.scoreCheck.setChecked(true);
                //当前项选中存储
//                double score = list.get(position).getScore();
//                int scoreId = list.get(position).getID();
//                int scoreType = 1;
//                if (q2Bean.getItemType()==1||TextUtils.isEmpty(q2Bean.getItemType()+"")){
//                    scoreType = 1;
//                }else{
//                    scoreType = q2Bean.getItemType();
//                }
//                if (rr!=null){
//                    int rr_ID = rr.get_ID();
//                    tempLostRR temp = new tempLostRR(rr_ID, MainActivity.id,schId,bean.getReportRowID(),score,scoreId,scoreType);
//                    try {
//                        db.saveOrUpdate(temp);
//                    } catch (DbException e) {
//                        e.printStackTrace();
//                    }
//                }else{
//
//                    tempLostRR cTemp = new tempLostRR(MainActivity.id,schId,bean.getReportRowID(),score,scoreId, BaseActivity.getNowDate(),q2Bean.getReportRowName(),scoreType);
//                    try {
//                        db.saveOrUpdate(cTemp);
//                    } catch (DbException e) {
//                        e.printStackTrace();
//                    }
//                }


//            }
//        }else{ //其他忽略
//            holder.scoreCheck.setChecked(false);
//        }



        return convertView;
    }

    class ViewHolder{
        RadioButton scoreCheck;
    }

}
